<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
/**
 * database row pager using datasets/tableobjects
 * This class outputs tabular data in a table broken down into pages
 *
 * @author Alton Crossley
 * @package Crossley Framework
 * @version 0.1
 */

class X_Data_Pager
{
    /**
     * the grid table
     *
     * @var X_DOM_Table
     */
    public $oGrid;
    
    /**
     * css class name added to the grid table
     *
     * @var string
     */
    public $sGridCssClass = 'X_grid';
    
    /**
     * collection to page
     *
     * @var X_Abstract_Object_Collection
     */
    private $_oDataX;
    /**
     * Adapter to collect from
     *
     * @var X_Interface_DB_Adapter
     */
    private $_oAdapter;
    /**
     * how many db rows will be in a page
     *
     * @var int
     */
    private $_iRowsPerPage = 10;
    /**
     * page for output (take it times $_iRowsPerPage for end row)
     *
     * @var unknown_type
     */
    private $_iCurrentPage = 1;
    
    /**
     * column names to hide
     *
     * @var array
     */
    protected $_aHideColumns = array();
    
    /**
     * field names of columns to show in the grid
     * Note: columns in $_aHideColumns are still hidden
     * Note: if any columns are defined, only the defined columns will be shown,
     * to show all columns, leave this and $_aHideColumns empty
     *
     * @var array
     */
    protected $_aShowColumns = array();
    
    function __construct ($xCollection, X_Interface_DB_Adapter $oAdapter = null)
    {
        $this->setCollection($xCollection);
        
        if ($oAdapter === null && $oAdapter = $this->_oDataX->getAdapter())
        {
            $this->setAdapter($oAdapter);
        }
        else if ($oAdapter instanceof X_Interface_DB_Adapter)
        {
            $this->setAdapter($oAdapter);
        }
        else
        {
            trigger_error('$oAdapter not set in constructor', E_USER_NOTICE);
        }
    
    }
    
    /**
     * save the adapor privately
     *
     * @param X_Interface_DB_Adapter $oAdapter
     */
    public function setAdapter (X_Interface_DB_Adapter $oAdapter)
    {
        $this->_oAdapter = $oAdapter;
    }
    
    /**
     * set collection
     *
     * @param unknown_type $xCollection string|X_Abstract_Object_Collection
     */
    public function setCollection ($xCollection)
    {
        if ($xCollection instanceof X_Abstract_Object_Collection)
        {
            $this->_oDataX = $xCollection;
        }
        elseif (is_string($xCollection) && class_exists($xCollection, true))
        {
            $this->_oDataX = new $xCollection();
        }
    }
    
    /**
     * set the rows per page to be paging
     *
     * @param int $iRowsPerPage
     */
    public function setPageSize ($iRowsPerPage)
    {
        $this->_iRowsPerPage = $iRowsPerPage;
    }
    
    /**
     * set the page number
     * note: $iPage X $iRowsPerPage
     *
     * @param int $iPage
     */
    public function setPage ($iPage)
    {
        $this->_iCurrentPage = $iPage;
    }
    
    public function setHideColums (Array $aColumns)
    {
        $this->_aHideColumns = $aColumns;
    }
    
    public function setShowColumns (Array $aColumns)
    {
        $this->_aShowColumns = $aColumns;
    }
    /**
     * build and return table grid
     *
     * @param int $iPage
     * @param int $iRowsPerPage
     * @return X_Data_Grid
     */
    public function getGrid ($iPage = null, $iRowsPerPage = null)
    {
        if (! empty($iPage))
        {
            $this->setPage($iPage);
        }
        if (! empty($iRowsPerPage))
        {
            $this->setPageSize($iRowsPerPage);
        }
        
        // get data
        $this->getData();
        
        // instance grid and fill it
        $this->oGrid = new X_Data_Grid();
        
        $this->oGrid->setHideColumns($this->_aHideColumns);
        $this->oGrid->setShowColumns($this->_aShowColumns);
        
        $this->oGrid->addData($this->_oDataX);
        
        // return grid
        


        return $this->oGrid;
    
    }
    
    /**
     * query data
     *
     */
    public function getData ()
    {
        $iCurrentEnd = ($this->_iRowsPerPage * $this->_iCurrentPage);
        $iCurrentStart = $iCurrentEnd - $this->_iRowsPerPage;
        
        // set limit
        $this->_oAdapter->addLimit($iCurrentStart, $this->_iRowsPerPage);
        
        if ($this->_oDataX instanceof X_Abstract_Object_Collection)
        {
            $this->_oDataX->setAdapter($this->_oAdapter);
            $this->_oDataX->gather();
        }
        elseif ($this->_oDataX instanceof X_Interface_Consumer)
        {
            $this->_oAdapter->fill($this->_oDataX);
        }
    }
}